package 斐波那契数列;

/**
 * 斐波那契数列
 * @author ycc
 * @date 2025/5/4
 */
public class Main2 {
    /**
     * 递归方式
     * @param n
     * @return
     * 1 1 2 3 5 8 13
     */
    public static int fib1(int n) {
        if(n < 2) {
            return n;
        }
        return fib1(n-1) + fib1(n-2);
    }

    /**
     * 循环实现
     * @param n
     * @return
     */
    public static int fib2(int n) {
        if(n < 2) {
            return n;
        }
        int first = 1;
        int second = 1;
        for(int i = 3; i <= n; i++) {
            second = first + second;
            first = second - first;
        }
        return second;
    }


    public static void main(String[] args) {
        System.out.println(fib1(6));
        System.out.println(fib2(6));
    }
}
